﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Utility;
using Utility.Service;

namespace Site
{
    public partial class About : UserControl
    {
        private int CurID = 0;

        public About()
        {
            InitializeComponent();
            this.myMenu.Logo = "1";
            this.Loaded += new RoutedEventHandler(About_Loaded);            
        }

        void About_Loaded(object sender, RoutedEventArgs e)
        {
            //菜单下滑动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = -83;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, myMenu);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin();
            InitLeftNav();
        }

        private void InitLeftNav()
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadItemsCompleted += new EventHandler<ReadItemsCompletedEventArgs>(client_ReadItemsCompleted);
            client.ReadItemsAsync(1);
        }

        void client_ReadItemsCompleted(object sender, ReadItemsCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    this.lblCur.Text = e.Result[this.CurID].Title;
                    this.htmlHost.SourceHtml = e.Result[this.CurID].Content;
                    
                    for (int i = 0; i < e.Result.Count; i++)
                    {
                        Button btn = new Button();
                        btn.Name = e.Result[i].ID.ToString();
                        btn.Click += new RoutedEventHandler(btn_Click);
                        btn.Style = Application.Current.Resources["styleLeftNavButton"] as Style;
                        btn.Content = e.Result[i].Title;
                        this.spLeftNav.Children.Add(btn);
                    }
                    this.LayoutRoot.UpdateLayout();
                }
            }
            else
            {
                throw e.Error;
            }
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            ServiceClient client = CUtility.GetServiceClient();
            client.ReadItemCompleted += new EventHandler<ReadItemCompletedEventArgs>(client_ReadItemCompleted);
            client.ReadItemAsync(Convert.ToInt32((sender as Button).Name));
        }

        void client_ReadItemCompleted(object sender, ReadItemCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    this.lblCur.Text = e.Result.Title;
                    this.htmlHost.SourceHtml = e.Result.Content;
                    this.LayoutRoot.UpdateLayout();
                }
            }
            else
            {
                throw e.Error;
            }
        }

        #region 面包屑导航
        private void lnkHome_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new Home());
        }

        private void lnkAbout_Click(object sender, RoutedEventArgs e)
        {
            App app = (App)Application.Current;
            app.RedirectTo(new About());
        }
        #endregion

    }
}
